home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / dpmigcc5.zip / RSX / SOURCE / DPMI / DPMIFUN.C next >
C/C++ Source or Header  |  1994-12-12  |  9KB  |  335 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <process.h>
  5. #include "..\DPMI.H"
  6. #include "..\DPMI10.H"
  7.  
  8. char file_desc[] =
  9. "**************************************************************************\n"
  10. "* FILE: dpmifun                                                          *\n"
  11. "*                                                                        *\n"
  12. "* DESC: shell for calling Dpmi functions                                 *\n"
  13. "*                                                                        *\n"
  14. "* Copyright (C) 1994                                                     *\n"
  15. "*      Rainer Schnitker, Heeper Str. 283, 33607 Bielefeld                *\n"
  16. "*      email: rainer@mathematik.uni-bielefeld.de                         *\n"
  17. "*                                                                        *\n"
  18. "* This program is distributed in the hope that it will be useful,        *\n"
  19. "* but WITHOUT ANY WARRANTY; without even the implied warranty of         *\n"
  20. "* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *\n"
  21. "* GNU General Public License for more details.                           *\n"
  22. "*                                                                        *\n"
  23. "**************************************************************************\n";
  24.  
  25. /* input function from input.c */
  26. int get_input(char *str, int size, int opt_newline);
  27.  
  28. typedef struct dpmifnct {
  29.     char *name;
  30.     int fnct;
  31.     int args;
  32.     char *help_text;
  33. } DPMIFNCT;
  34.  
  35. DPMIFNCT dpmif[] = {
  36.     #include "dpmifun.h"
  37. };
  38.  
  39. #define N_REGS 10   /* we could use regs 0 - regs 9 */
  40. int regs[128];        /* so we have 512 bytes for functions */
  41.  
  42. /*
  43. **  Test DPMI server
  44. */
  45. #ifdef __EMX__
  46. static int test_dpmi_server(void)
  47. {
  48.     return ((_emx_env & 0x80) | (_emx_env & 0x100));
  49. }
  50. #endif
  51.  
  52. #ifdef __GO32__
  53. #include <go32.h>
  54. extern Go32_Info_Block _go32_info_block;
  55.  
  56. static int test_dpmi_server(void)
  57. {
  58.     return _go32_info_block.run_mode & _GO32_RUN_MODE_DPMI;
  59. }
  60. #endif
  61.  
  62.  
  63. void PrintDescriptor(LPDESCRIPTOR d)
  64. {
  65.     BYTE flag;
  66.  
  67.     if (!(d->access & PRESENT_BIT)) {
  68.     printf("Not Present\n");
  69.     return;
  70.     }
  71.     if (d->access & SEGMENT_BIT)/* code or data selector */
  72.     printf("Base=%02X%02X%04X Limit=%01X%04X %s DPL=%u %s %s %s %s\n",
  73.            d->base_hi, d->base_mi, d->base_lo,
  74.            d->lim_hi & LIMIT_HI_MASK, d->lim_lo,
  75.            (d->lim_hi & GRANULAR_BIT) ? "Pages" : "Bytes",
  76.            (d->access & DPL_MASK) >> 5,
  77.            (d->lim_hi & DEFAULT_BIT) ? "32bit" : "16bit",
  78.            (d->access & CODE_BIT) ?
  79.            (d->access & CONFIRMING_BIT) ? "CONF" : "CODE" :
  80.            (d->access & EXPAND_BIT) ? "EXPD" : "DATA",
  81.            (d->access & WRITE_BIT) ?
  82.            (d->access & CODE_BIT) ? "R" : "W" : "-",
  83.            (d->access & ACCESS_BIT) ? "ACC" : " ");
  84.  
  85.     else {            /* system selector */
  86.     flag = d->access & (BYTE) 7;
  87.     switch (flag) {
  88.     case 0:
  89.         break;
  90.     case 1:
  91.         printf("TSS%s ", (d->access & 8) ? "386" : "286");
  92.         break;
  93.     case 2:
  94.         printf("LDT ");
  95.         break;
  96.     case 3:
  97.         printf("BUSYTSS%s ", (d->access & 8) ? "386" : "286");
  98.         break;
  99.     case 4:
  100.         printf("CALLGATE%s ", (d->access & 8) ? "386" : "286");
  101.         break;
  102.     case 5:
  103.         printf("TASKGATE ");
  104.         break;
  105.     case 6:
  106.         printf("INT_GATE%s ", (d->access & 8) ? "386" : "286");
  107.         break;
  108.     case 7:
  109.         printf("TRAPGATE%s ", (d->access & 8) ? "386" : "286");
  110.         break;
  111.     }
  112.  
  113.  
  114.     if (flag <= 3) {
  115.         printf("Base=%02X%02X%04X Limit=%01X%04X DPL=%u\n",
  116.            d->base_hi, d->base_mi, d->base_lo,
  117.            d->lim_hi & LIMIT_HI_MASK, d->lim_lo,
  118.            (d->access & DPL_MASK) >> 5);
  119.     } else {
  120.         printf("Sel=%04X Offset=%02X%02X%04X DPL=%u\n",
  121.            d->base_lo,
  122.            d->base_hi, d->lim_hi, d->lim_lo,
  123.            (d->access & DPL_MASK) >> 5);
  124.     }
  125.     }                /* else system descriptor */
  126. }
  127.  
  128. void do_help(char *s)
  129. {
  130.     int i;
  131.     for (i = 0; dpmif[i].name ; i++)
  132.     if (stricmp(dpmif[i].name, s) == 0) {
  133.         puts(dpmif[i].help_text);
  134.         return;
  135.     }
  136.     for (i = 0; dpmif[i].name ; i++)
  137.     if (strstr(dpmif[i].name, s))
  138.         puts(dpmif[i].help_text);
  139. }
  140.  
  141. static int make_tokens(char *argmem, char **argp)
  142. {
  143.     int i;
  144.     int args=0;
  145.  
  146.     argp[args++] = argmem;
  147.     for (i = 0; *(argmem + i) != 0; i++)
  148.     if (*(argmem + i) == ' ') {
  149.         *(argmem + i) = 0;
  150.         if (*(argmem + i + 1) == ' ')
  151.         continue;
  152.         if (*(argmem + i + 1) == 0)
  153.         break;
  154.         argp[args++] = (argmem + i + 1);
  155.     }
  156.     argp[args] = 0;
  157.  
  158.     return args;
  159. }
  160.  
  161. static int get_function(char *s, int *argc)
  162. {
  163.     int i;
  164.  
  165.     for (i = 0; dpmif[i].name ; i++)
  166.     if (strcmp(dpmif[i].name, s) == 0) {
  167.         *argc = dpmif[i].args;
  168.         return (int) dpmif[i].fnct;
  169.     }
  170.     return 0;
  171. }
  172.  
  173. static int my_atoi(char *s)
  174. {
  175.     int c;
  176.     if ( (*s) < '0' || (*s) > '9')
  177.     return -1;
  178.     return ((c=atoi(s)) >= N_REGS) ? -1 : c;
  179. }
  180.  
  181. static int hex_atoi(char *s)
  182. {
  183.     int c;
  184.     if ( (*s) < '0' || (*s) > '9')
  185.     return 0;
  186.     if (*(s+1)=='x')
  187.     sscanf(s+2,"%x\n", &c);
  188.     else
  189.     sscanf(s,"%d\n", &c);
  190.     return c;
  191. }
  192.  
  193. static int push_args(int begin, int end, char **argv, int pushv[])
  194. {
  195.     int i;
  196.  
  197.     for (i = begin; i <= end; i++) {
  198.     if (argv[i][0] == 'r') {
  199.         int n = my_atoi(& argv[i][1]);
  200.         if (n<0)
  201.         return n;
  202.         pushv[i] = regs[n];
  203.     }
  204.     else if (argv[i][0] == '&' && argv[i][1] == 'r') {
  205.         int n = my_atoi(& argv[i][2]);
  206.         if (n<0)
  207.         return n;
  208.         pushv[i] = (int) & regs[n];
  209.     }
  210.     else
  211.         pushv[i] = hex_atoi(argv[i]);
  212.     }
  213.     return 0;
  214. }
  215.  
  216. int execute_dpmi(int fnct, int p1, int p2, int p3, int p4, int p5);
  217.  
  218. int main(int argc, char **argv, char **env)
  219. {
  220.     char cmd[80];        /* cmd line */
  221.     char * argvec[20];        /* cmd tokens */
  222.     int push[20];        /* pushed args for dpmi */
  223.     unsigned fnct;        /* dpmi function */
  224.     int n,p,ret;
  225.  
  226.     puts(file_desc);
  227.     puts("help command: help or h\n");
  228.  
  229.     if (!test_dpmi_server()) {
  230.     puts("I need dpmi, run this program with a dpmiextender\n");
  231.     puts("EMX: run rsx ; DJGPP run go32 not with 'nodpmi'\n");
  232.     return 1;
  233.     }
  234.  
  235.     for (;;) {
  236.     printf("(DPMI):");
  237.     fflush(stdout);
  238.     get_input(cmd, sizeof(cmd), 1);
  239.     n = make_tokens(cmd, argvec);
  240.  
  241.     switch (*cmd) {
  242.         case '0':
  243.         case 'q':
  244.         printf("bye\n");
  245.         return 0;
  246.  
  247.         case '\n':
  248.         case '\r':
  249.         continue;
  250.  
  251.         case 'h':
  252.         if (argvec[1])
  253.             do_help(argvec[1]);
  254.         else {
  255.             printf("\n"
  256.             "(h)elp <function> - get help\n"
  257.             "c[0-9]            - clear regs 0-9\n"
  258.             "r[0-9]            - print regs 0-9\n"
  259.             "s[0-9] <value>    - set regs 0-9 to v\n"
  260.             "DPMIFUNCTION args - execute dpmifuncion\n"
  261.             "! <args>          - execute shell\n"
  262.             "(q)uit            - quit\n"
  263.             "Arrow keys <up>,<down>,<left>,<right>\n"
  264.             );
  265.  
  266.             printf("\nExample:\n"
  267.             "dpmi:help DpmiGet                 // list all functions with DpmiGet\n"
  268.             "int  DpmiGetCoproStatus (UNIT *)  // answer\n"
  269.             "dpmi:DpmiGetCoproStatus &r1       // call DPMI function with address r1\n"
  270.             "return 0 = 0x0                    // no error\n"
  271.             "dpmi:r1                           // display register 1\n"
  272.             "dpmi:GetDescriptor 0xf7 &r1       // use r1-r4 as a buffer\n"
  273.             "return 0 = 0x0                    // buffer can use 512 bytes */\n"
  274.             "dpmi:PrintDescriptor &r1\n"
  275.             "Base=80003000 Limit=22FFF Bytes DPL=3 32bit CODE R ACC\n"
  276.             );
  277.         }
  278.         continue;
  279.  
  280.         case 'c':
  281.         n = my_atoi(& argvec[0][1]);
  282.         if (n < 0)
  283.             puts("What?");
  284.         else
  285.             regs[n] = 0;
  286.         continue;
  287.  
  288.         case 'r':
  289.         n = my_atoi(& argvec[0][1]);
  290.         if (n < 0)
  291.             puts("What?");
  292.         else
  293.             printf("Reg%d: %d = 0x%X\n", n, regs[n], regs[n]);
  294.         continue;
  295.  
  296.         case 's':
  297.         n = my_atoi(& argvec[0][1]);
  298.         if (n < 0)
  299.             puts("What?");
  300.         else {
  301.             p = hex_atoi(argvec[1]);
  302.             regs[n] = p;
  303.             printf("Reg%d: %d = 0x%X\n", n, regs[n], regs[n]);
  304.         }
  305.         continue;
  306.  
  307.         case '!':
  308.         if (argvec[1] == NULL)
  309.             system("");
  310.         else if (spawnvpe(P_WAIT, argvec[1], & argvec[1], env)==-1)
  311.             perror("shell program:");
  312.         continue;
  313.  
  314.         default:
  315.         fnct = get_function(argvec[0], &p);
  316.         if (!fnct) {
  317.             printf("not a dpmi function\n");
  318.             continue;
  319.         }
  320.         if (n-1 != p) {
  321.             printf("Illegal argument count\n");
  322.             continue;
  323.         }
  324.         push[0] = fnct;
  325.         if (push_args(1, n-1, argvec, push)<0) {
  326.             printf("bad parameter\n");
  327.             continue;
  328.         }
  329.         ret=execute_dpmi(push[0],push[1],push[2],push[3],push[4],push[5]);
  330.         printf("return %d = 0x%X\n", ret, ret);
  331.         regs[0] = ret;    /* r0 = return register */
  332.     }   /* switch */
  333.     }
  334. }
  335.